import { _decorator, Component, Node,Quat, quat, Sprite, tween, v3, Vec3,Color, color } from 'cc';
import {MoneyAnim} from "./MoneyAnim";
import {GameMain} from "./GameMain";
import {AudioClipID, AudioManager} from "./AudioManager";
import Platform from "./Platform";
const { ccclass, property } = _decorator;

@ccclass('WXShareUI')
export class WXShareUI extends Component
{
    @property({type:Node})
    targetNode:Node=null;

    @property({type:MoneyAnim})
    moneyAnim:MoneyAnim=null;

    private lightSprite:Sprite=null;
    private lightRotate:number=0;
    private lightQuat:Quat=quat(0,0,0,0);
    private inited:boolean=false;

    public btnClicked:boolean=false;

    onLoad()
    {
        this.init();
    }

    public onShareClicked()
    {
        this.btnClicked=true;
        Platform.shareApp();
    }

    public init()
    {
        this.lightSprite=this.targetNode.getChildByName("rotateLight").getComponent(Sprite);
        this.btnClicked=false;
        this.inited=true;
    }

    start() {

    }

    public show()
    {
        this.btnClicked=false;
        if(this.node.active)
            return;
        if(!this.inited)
            this.init();

        //AudioManager.playClip(AudioClipID.Tip);

        this.node.active=true;
        this.targetNode.setScale(0.4, 0.4,0.4);
        tween(this.targetNode)
            .to(0.3,{scale:Vec3.ONE}, { easing: 'backOut' })
            .start();
    }

    public close()
    {
        //this.node.active=false;

        tween(this.targetNode).to(0.2,{scale:v3(0.5, 0.5, 0.5)}).call(()=>{
            this.node.active=false;
            GameMain.instance.resultUI.show();
        }).start();
    }

    public onWXShow(timeInterval:number)
    {
        if(!this.btnClicked)
            return;
        if(timeInterval>5000)
        {
            this.moneyAnim.setDstNode(GameMain.instance.mainUI.moneyNode);
            this.moneyAnim.play(()=>{

            });
            //获取钻石
            GameMain.instance.gameData.addMoney(188);
            GameMain.instance.mainUI.updateMoneyLabel();

            this.scheduleOnce(()=>{
                AudioManager.playClip(AudioClipID.Coin);
            }, 0.3);

            this.scheduleOnce(()=>{
                this.close();
            }, 0.8);
        }
        else
        {
            wx.showToast({
                title: '请分享到不同的群',
                icon: 'error',
                duration: 2500
            });
        }
        this.btnClicked=false;
    }

    update(dt: number)
    {
        if(this.lightSprite.node.active)
        {
            this.lightRotate-=dt * 35;
            Quat.fromEuler(this.lightQuat, 0, 0,this.lightRotate);
            this.lightSprite.node.setRotation(this.lightQuat);
        }
    }
}

